var socket = new WebSocket("ws://localhost:3000/");
var showLog = null;
var clientNumber = 0;

function updateLink() {
	user = document.getElementById("userName").value;
	elements = document.getElementsByClassName("singleLink");

	for(i in elements) {
		elements[i].href += "?user=" + user;
	}

}

function askView() {
	askLog();
	//socket.send("messge");
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", "http://localhost:3000/images/all", true);
	xmlhttp.onreadystatechange = function() {

		if(xmlhttp.readyState == 3) {
		};
		if(xmlhttp.readyState == 4) {
			renderView(xmlhttp.responseText, "view");
		};
	}
	xmlhttp.send(null);
}

function askSearch(event, value) {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", "http://localhost:3000/view/search/?descr=" + value, true);
	xmlhttp.onreadystatechange = function() {

		if(xmlhttp.readyState == 3) {
		};
		if(xmlhttp.readyState == 4) {
			renderView(xmlhttp.responseText, "view");
		};
	}
	xmlhttp.send(null);

	//askView();

}

function askLog() {

	clearInterval(showLog)
	showLog = setInterval(askLog, 10000)

	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", "http://localhost:3000/view/logs", true);
	xmlhttp.onreadystatechange = function() {

		if(xmlhttp.readyState == 3) {
		};
		if(xmlhttp.readyState == 4) {
			renderView(xmlhttp.responseText, 'log');
		};
	}
	xmlhttp.send(null);

}

function renderView(text, id) {
	document.getElementById(id).innerHTML = text
}

function sendMessage() {
	socket.send("message");
}

socket.onmessage = function(msg) {

	if (msg.data.length == 0){
		askLog();
	}
	
	else {
	alert("you should refresh your page")}
	
}





